package server

import (
	core "carmanager/db"
	model "carmanager/models"
	"carmanager/models/dto"
	"carmanager/models/vo"
	"carmanager/util"
	"strings"
	"time"

	"gorm.io/gorm"
)

type CarService struct{}
type Result struct {
	TotalNumber int64 `gorm:"column:totalNumber`
}

func (c *CarService) List(dto dto.CarDto, page dto.PageParms) (bool, vo.PageInfo) {
	db := core.GetDB()
	sql := `select t1.id,timestampdiff(hour ,now(),t1.placeStartTime)*t2.price as paidPrice,
		 t1.number_plate,t3.NickName, t1.create_time, t1.update_time, t1.user_id,
	 	t1.placeStartTime, t1.placeStopTime, t1.placeMemory, t1.isPlay, 
		 t1.isOutBound, t1.placeid,t2.enc 
		from parking_car t1 inner join parking_place t2 inner join sys_user t3 
		on t1.placeid=t2.id and t1.user_id = t3.id where 1=1`

	if dto.UserId != 0 {
		sql += " and t1.user_id=@UserId"
	}
	if dto.PlaceId != 0 {
		sql += " and t1.placeid=@PlaceId"
	}
	ctnsql := strings.Clone(sql)
	if !strings.EqualFold(dto.NumberPlate, "") {
		dto.NumberPlate = "%" + dto.NumberPlate + "%"
		ctnsql += " and t1.number_plate like '" + dto.NumberPlate + "'"
		sql += " and t1.number_plate like @NumberPlate"
	}

	//获取长度
	result := new([]vo.CarVo)
	//很恶心 他不支持只能用这种办法
	page.PageIndex = (page.PageIndex - 1) * page.PageSize

	if db.Raw(strings.Clone(ctnsql)).Scan(result).Error != nil {
		return false, vo.PageInfo{}
	}

	result1 := new([]vo.CarVo)

	sql += " order by  t1.placeStartTime desc"
	sql += " limit @PageIndex,@PageSize"
	if db.Raw(strings.Clone(sql), dto, page).Scan(result1).Error != nil {
		return false, vo.PageInfo{}
	}
	return true, vo.PageInfo{
		Data:        result1,
		TotalNumber: int64(len(*result)),
	}
}

func (c *CarService) Add(model model.Car) bool {
	db := core.GetDB()
	now := time.Now()
	model.CreateTime = &now
	model.UpdateTime = &now
	return db.Create(&model).Error != nil
}

// 修改停车记录
func (c *CarService) Update(model model.Car) bool {
	db := core.GetDB()
	now := time.Now()
	model.UpdateTime = &now

	return db.Updates(&model).Error != nil
}

// 添加停车记录
func (c *CarService) StopCar(Dto dto.CarDto) bool {
	PlaceStartTime := time.Now()
	Car := model.Car{
		UserID:         Dto.UserId,
		NumberPlate:    Dto.NumberPlate,
		PlaceStartTime: &PlaceStartTime,
		IsPlay:         util.PlayNo,
		PlaceId:        Dto.PlaceId,
		IsOutBound:     util.OutBoudNo,
		CreateTime:     &PlaceStartTime,
		UpdateTime:     &PlaceStartTime,
	}
	place := model.Place{
		ID:    Dto.PlaceId,
		State: 1,
	}
	err := core.Db.Transaction(func(tx *gorm.DB) error {
		if err := core.Db.Updates(&place).Error; err != nil {
			return err
		}
		if err := core.Db.Create(&Car).Error; err != nil {
			return err
		}
		return nil
	})
	return err == nil

}

// 删除该记录
func (c *CarService) Del(carid int) bool {
	return core.Db.Delete("id=?", carid).Error != nil
}

// 出库
func (c *CarService) OutBoud(carid int) bool {

	sql := "select timestampdiff(hour ,now(),placeStartTime)*price as PlacePrice from parking_car inner join parking_place on placeid=parking_place.id where parking_car.id=?"
	PlacePrice := new(float64)
	now := time.Now()
	core.Db.Raw(sql, carid).Scan(PlacePrice) //获取停车费用
	car := model.Car{
		ID:             carid,
		PlaceMemory:    *PlacePrice,
		IsOutBound:     util.OutBoudYes,
		IsPlay:         util.PlayYes,
		PlaceStartTime: &now,
	}

	sql = "select parking_place.id from parking_car inner join parking_place on placeid=parking_place.id where parking_car.id=?"
	parkingPlaceId := new(float64)
	core.Db.Raw(sql, carid).Scan(parkingPlaceId) //获取停车费用
	place := model.Place{
		ID:    int(*parkingPlaceId),
		State: 2,
	}
	core.Db.Updates(place)
	return core.Db.Updates(&car).Error == nil
}
